import { ClassSerializerInterceptor, PlainLiteralObject, StreamableFile } from "@nestjs/common";
import { ClassTransformOptions } from "class-transformer";
import { isArray, isNil, isObject } from 'lodash';
export class AppInterceptor extends ClassSerializerInterceptor{
    Serializer(Value:PlainLiteralObject|Array<PlainLiteralObject>,options: ClassTransformOptions):PlainLiteralObject|Array<PlainLiteralObject>{
        //如果不是数组和对像或者匹配为数据流进行原样返回
        if(!isObject(Value)&&!isArray(Value)|| Value instanceof StreamableFile){
            return Value
        }
        //如果是个数组
        if(isArray(Value)){
            return Value.map(item=>{
                isObject(Value)? this.transformToPlain(Value,options) :item
            })
        }
         // 如果是分页数据,则对items中的每一项进行序列化
         if('mate' in Value&& 'items' in Value){
        const items=  isNil(Value.items)&&isArray(Value.items) ? Value.items : []
          return {
            ...Value,
          items:(items as Array<PlainLiteralObject>).map(item=>{
            isObject(item)?this.transformToPlain(item,options):item
          })
          }
         }
         // 如果响应是个对象则直接序列化
         return this.transformToPlain(Value,options)
    }
}